﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PiControls.Controls.ShowImage
{
    public partial class PShowImage: UserControl
    {
        private float scaleFactor = 1.0f; // 缩放比例
        private readonly float scaleStep = 0.1f; // 缩放步长
        private Bitmap _originalImage;
        private bool isDragging = false; // 是否正在拖拽
        private Point lastMousePosition; // 记录鼠标上一次的位置

        public Bitmap Image {

            get { return _originalImage; }

            set
            {
                if (value != null)
                {
                    pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                    _originalImage = value;
                    pictureBox1.Image = _originalImage;
                    pictureBox1.Size = _originalImage.Size;
                }
                else
                {
                    _originalImage = null;
                }
            }

        } // 原始图片
        public PShowImage()
        {
            InitializeComponent();
            InitializePictureBox();
        }

        private void InitializePictureBox()
        {
            // 初始化 PictureBox
            //pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            //originalImage = Image.FromFile("d:/test2.png"); // 替换为你的图片路径
            //pictureBox1.Image = originalImage;
            //pictureBox1.Size = originalImage.Size;

            // 绑定鼠标事件
            pictureBox1.MouseWheel += PictureBox1_MouseWheel; // 滚轮缩放
            pictureBox1.MouseDown += PictureBox1_MouseDown;   // 鼠标按下
            pictureBox1.MouseMove += PictureBox1_MouseMove;   // 鼠标移动
            pictureBox1.MouseUp += PictureBox1_MouseUp;       // 鼠标松开
        }

        // 鼠标滚轮缩放
        private void PictureBox1_MouseWheel(object sender, MouseEventArgs e)
        {

            if (Image == null)
            {
                return;
            }
            if (e.Delta > 0)
            {
                scaleFactor += scaleStep; // 放大
            }
            else if (scaleFactor > scaleStep)
            {
                scaleFactor -= scaleStep; // 缩小
            }
            ApplyZoom();
        }

        // 鼠标按下事件，记录起始点
        private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = true;
                lastMousePosition = e.Location;
                pictureBox1.Cursor = Cursors.Hand;
            }
        }


        // 鼠标移动事件，实现拖拽
        private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                int dx = e.X - lastMousePosition.X;
                int dy = e.Y - lastMousePosition.Y;

                pictureBox1.Left += dx;
                pictureBox1.Top += dy;
            }
        }


        // 鼠标释放事件，停止拖拽
        private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                isDragging = false;
                pictureBox1.Cursor = Cursors.Default;
            }
        }

        // 应用缩放
        private void ApplyZoom()
        {


            int newWidth = (int)(Image.Width * scaleFactor);
            int newHeight = (int)(Image.Height * scaleFactor);
            pictureBox1.Size = new Size(newWidth, newHeight);
        }
    }
}
